*********************************************************************
8/9/2011: ADDED SORT BY COUNTER TO BE CONSISTENT WITH SORT BY PPOS IN LATER
		  YEARS.  IT DID NOT AFFECT ANY OBS.

5/4/11: ADDED TOT_TRANS AND SSINC AND CACLUALTE AT FAMILY LEVEL

1/1/11: EDITED CALC OF TS_TOTTAX TO REFLECT THAT FICA IS CALLED FICA,
		NOT TS_FICA IN SY 1972

8/22/07: I converted this to a macro and added the tax variables.

THESE NOTES WERE IN THE PROGRAM, BUT FOR SOME REASONS, THIS SCREWS UP
THE MACRO, SO I'VE MOVED THEM TO THE TOP
*never married, divorced, separated, or widowed 
*at least one own never-married kid under 18 
*primary families or unrelated subfamilies 
* For survey years 1972-1975 there's no fkind variable, so create hwfam
*msupwgt is wrong becasue does not include negatives
*get identifiers for primary families without other adults or other families
*going to give the sm status of the primary family to the related subfamily for all sm to match
broader CE definition
* exclude the related subfamilies because their values of the following are 
  already included in the values for the primary family
**********************************************************************;

options obs=max;

%macro clean7375(yr);

data poverty;
   set cpsdata.all&yr;
   by famid;

RETAIN counter 0; 
counter= counter+1;
IF FIRST.famid THEN DO; 
   counter=1;
END;

id=famid*100+counter;

IF 2 LE wkswrk LE 7 THEN emp=1;
  ELSE emp=0; 
kid=(age LE 17);

tot_trans=ssinc+pawval+uiinc;

run;

PROC SORT DATA=taxsim.new_taxs&yr;
   BY id;
RUN;
DATA poverty;
MERGE poverty(IN=in1) taxsim.new_taxs&yr(DROP=year);
BY id;
IF in1;

IF &yr=1972 THEN ts_tottax=ftl+taxs+(fica/2);
   ELSE IF 1973 LE &yr LE 1977 THEN ts_tottax=ftl+taxs+(ts_fica/2);

run;

proc means sum noprint data=poverty;
   by famid;
   var ts_tottax emp kid tot_trans ssinc;
   output out=tmp1(drop= _type_ _freq_) sum=ftax_nonprime no_earnr_nonprime no_kids_nonprime
											ftot_trans_nonprime fssinc_nonprime;
run;

DATA poverty;
MERGE poverty(IN=in1) tmp1(KEEP=famid ftax_nonprime no_earnr_nonprime no_kids_nonprime
								ftot_trans_nonprime fssinc_nonprime);
BY famid;
IF in1;
run;

PROC SORT DATA=poverty;
   BY hhid;
RUN;
proc means sum noprint data=poverty;
   by hhid;
   where ftype in (2,4);
   var ts_tottax emp kid tot_trans ssinc;
   output out=tmp2(drop= _type_ _freq_) sum=ftax_prime no_earnr_prime no_kids_prime ftot_trans_prime fssinc_prime; 
run;
proc means sum noprint data=poverty;
   by hhid;
   var ts_tottax tot_trans ssinc;
   output out=tmp3(drop= _type_ _freq_) sum=hts_tottax htot_trans hssinc_rr;
run;

DATA poverty;
MERGE poverty(IN=in1) tmp2(KEEP=hhid ftax_prime no_earnr_prime no_kids_prime ftot_trans_prime fssinc_prime) 
					  tmp3(KEEP=hhid hts_tottax htot_trans hssinc_rr);
BY hhid;
IF in1;

IF ftype IN(2,4) THEN fts_tottax=ftax_prime;
   ELSE fts_tottax=ftax_nonprime;
IF ftype IN(2,4) THEN tot_earnrs=no_earnr_prime;
   ELSE tot_earnrs=no_earnr_nonprime;
IF ftype IN(2,4) THEN tot_kids=no_kids_prime;
   ELSE tot_kids=no_kids_nonprime;
IF ftype IN(2,4) THEN ftot_trans_final=ftot_trans_prime;
   ELSE ftot_trans_final=ftot_trans_nonprime;
IF ftype IN(2,4) THEN fssinc_rr_final=fssinc_prime;
   ELSE fssinc_rr_final=fssinc_nonprime;

run;

data temp0;
      set poverty; 
if ftype in (2,4);
run;

proc sort data=temp0;
by famid;
run;

data temp0;
set temp0;
by famid;
retain ftotval2 fpovcut2 ftotearn2 fpersons2 frelu182;
if first.famid and ftype=2 then do;
ftotval2=ftotval;
fpovcut2=fpovcut;
ftotearn2= ftotearn;
fpersons2=fpersons;
frelu182=frelu18;

end;
if last.famid and ftype=4 then output;
run;

/* COUNTER IS ACTING AS PERSON ID (LIKE PPOS IN LATER YEARS */
PROC SORT DATA=poverty;
   BY famid counter;
RUN;

data temp;
   set poverty;
   by famid counter;
   if first.famid; 

   IF marstat IN (1,3,6,7) AND fownu18 GT 0 AND ftype IN (1,2,4) 
      and sex=2 and 18 LE age LE 54 then sm_f=1;
      ELSE sm_f=0;

   if marstat=2 AND fpersons>1 THEN hwfam=1;
   ELSE hwfam=0;

IF 2 LE wkswrk LE 7 THEN emp_hd_nprime=1;
  ELSE emp_hd_nprime=0; 

age_hd_nprime=age;

  IF age LE 64 AND tot_kids GT 0 AND marstat IN (0,1,3,6,7) THEN dem_grp_nprime=1;
   ELSE IF age LE 64 AND tot_kids GT 0 AND marstat IN (2,4,5) THEN dem_grp_nprime=2;
   ELSE IF age LE 64 AND tot_kids=0 AND marstat IN (0,1,3,6,7) THEN dem_grp_nprime=3;
   ELSE IF age LE 64 AND tot_kids=0 AND marstat IN (2,4,5) THEN dem_grp_nprime=4;
   ELSE IF age GE 65 THEN dem_grp_nprime=5;

   IF hga LE 12 OR (hga=13 AND hgc NE 1) THEN ed_hd_nprime=1;
      ELSe IF hga=13 AND hgc=1 THEN ed_hd_nprime=2;
      ELSE IF 14 LE hga LE 16 OR (hga=17 AND hgc NE 1) THEN ed_hd_nprime=3;
      ELSE IF hga GT 17 OR (hga=17 AND hgc=1) THEN ed_hd_nprime=4;

run;


data poverty;
merge poverty temp(keep=famid sm_f hwfam emp_hd_nprime age_hd_nprime dem_grp_nprime ed_hd_nprime) 
			  temp0(keep=famid ftotval2 fpovcut2 ftotearn2 fpersons2 frelu182);
by famid;
if ftype=4 then ftotval_final=ftotval2;
else ftotval_final=ftotval;
if ftype=4 then fpovcut_final=fpovcut2;
else fpovcut_final=fpovcut;
if ftype=4 then ftotearn_final=ftotearn2;
else ftotearn_final=ftotearn;
if ftype=4 then fpersons_final=fpersons2;
else fpersons_final=fpersons;
if ftype=4 then frelu18_final=frelu182;
else frelu18_final=frelu18;

msupwgt=msupwgt3;
run;

proc sort data=poverty;
   by hhid famid counter;
run;

proc means max noprint data=poverty;
by hhid;
var famrel2;
   output out=temp3 max=fammax;
run;

data temp2;
   set temp(rename=(sm_f=sm_h));
   IF ftype IN(1,2);
   keep hhid sm_h;
run;

proc means sum noprint data=temp;
   by hhid;
   where ftype NE 4;
   var ftotearn fpersons frelu18;
   output out=temp1(drop= _type_ _freq_) sum=htotearn hpersons hrelu18;
run;

data temp4;
   set poverty;
   by hhid famid counter;
   if first.hhid; 

IF 2 LE wkswrk LE 7 THEN emp_hd_prime=1;
  ELSE emp_hd_prime=0; 

age_hd_prime=age;

  IF age LE 64 AND tot_kids GT 0 AND marstat IN (0,1,3,6,7) THEN dem_grp_prime=1;
   ELSE IF age LE 64 AND tot_kids GT 0 AND marstat IN (2,4,5) THEN dem_grp_prime=2;
   ELSE IF age LE 64 AND tot_kids=0 AND marstat IN (0,1,3,6,7) THEN dem_grp_prime=3;
   ELSE IF age LE 64 AND tot_kids=0 AND marstat IN (2,4,5) THEN dem_grp_prime=4;
   ELSE IF age GE 65 THEN dem_grp_prime=5;

   IF hga LE 12 OR (hga=13 AND hgc NE 1) THEN ed_hd_prime=1;
      ELSe IF hga=13 AND hgc=1 THEN ed_hd_prime=2;
      ELSE IF 14 LE hga LE 16 OR (hga=17 AND hgc NE 1) THEN ed_hd_prime=3;
      ELSE IF hga GT 17 OR (hga=17 AND hgc=1) THEN ed_hd_prime=4;

KEEP hhid emp_hd_prime age_hd_prime dem_grp_prime ed_hd_prime;
run;



data cpsdata.poverty&yr;
   merge poverty temp1(keep=hhid htotearn hpersons hrelu18) 
				 temp2(keep=hhid sm_h) 
				 temp3(keep=hhid fammax)
				 temp4(keep=hhid emp_hd_prime age_hd_prime dem_grp_prime ed_hd_prime);
   by hhid;
   IF ftype ne 4 then sm=sm_f;
   ELSE IF ftype=4 then sm=sm_h;

   IF ftype=1 and sm=1 and fammax LE 9 then smlone=1;
   ELSE smlone=0;

   IF 1 LE state2 LE 31 THEN hreg=1;
      ELSE IF 32 LE state2 LE 49 THEN hreg=2;
	  ELSE IF 51 LE state2 LE 79 THEN hreg=3;
	  ELSE IF state2 GE 80 THEN hreg=4;


IF ftype IN(2,4) THEN DO;
	dem_grp=dem_grp_prime;
	ed_head=ed_hd_prime;
	emp_head=emp_hd_prime;
	age_head=age_hd_prime;
END;
IF ftype IN(1,3,5,6,7,8,9) THEN DO;
	dem_grp=dem_grp_nprime;
	ed_head=ed_hd_nprime;
	emp_head=emp_hd_nprime;
	age_head=age_hd_nprime;
END;

run;

proc means;
run;


%mend;
%clean7375(1973);
%clean7375(1974);
%clean7375(1975);

